package 力扣._394_字符串解码;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Solution {
    //这个方法并不可以，因为【】可能嵌套，还是得用栈来解决
    //直接正则表达式
    public String decodeString(String s) {
        String regex = "([a-z]*)([\\d]*)(\\[([a-z]*)\\])?";
        Pattern compile = Pattern.compile(regex);
        Matcher matcher = compile.matcher(s);
        StringBuilder result = new StringBuilder();
        while (matcher.find()) {
            String s1 = matcher.group(1);
            int c = 0;
            if (matcher.group(2).length() != 0)
                c = Integer.parseInt(matcher.group(2));
            String s2="";
            s2 += matcher.group(4);
            result.append(s1).append(s2.repeat(c));
        }
        return result.toString();
    }

    public static void main(String[] args) {
        String s = new Solution().decodeString("abc3[cd]xyz");
        System.out.println(s);
    }
}
